﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views;
using DevExpress.XtraGrid.Views.Base;
using COMOSOFT.Class;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmImportTrimList : Form
    {
        ClassShare cls = new ClassShare("FrmImportTrimList");
        FrmStyleDetails fr_SD = null;
        int? MALID;
        int? TempMALID;
        string display = null;
        string value = null;
        string displaycolor = null;
        string valuecolor = null;
        public FrmImportTrimList(FrmStyleDetails _SD,int _MALID)
        {
            InitializeComponent();
            this.fr_SD = _SD;
            this.MALID = _MALID;
        }      

        private void gridLookUp_TrimListMaster_Click(object sender, EventArgs e)
        {
            sp_MAImport_MALMasterTableAdapter.Fill(dataSetMatarial.sp_MAImport_MALMaster,MALID);            
        }

        private void bnt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridLookUp_TrimListMaster_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUp_TrimListMaster.EditValue != null)
            {
                display = null;
                TempMALID =Convert.ToInt32( gridLookUp_TrimListMaster.EditValue);                
                sp_MAImport_MASDTableAdapter.Fill(dataSetMatarial.sp_MAImport_MASD, TempMALID);
            }
        }

        private void gridLook_MASource_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            try
            {
                GridView view = gridLook_MASource.Properties.View as GridView;
                if (view.FocusedRowHandle >= 0)
                {                   
                    int[] rows = view.GetSelectedRows();
                    display = null;
                    value = null;
                    if (rows.Count() > 0)
                    {
                        foreach (int i in rows)
                        {
                            if (!view.IsGroupRow(i))
                            {
                                display += view.GetDataRow(i)["Name"].ToString() + '-' + view.GetDataRow(i)["Color"].ToString() + ", ";
                                value += view.GetDataRow(i)["MALDID"].ToString() + ", ";
                            }
                        }
                    }
                    if (display != null)
                    {
                        display = display.Substring(0, display.Length - 2);
                        value = value.Substring(0, value.Length - 2);
                    }
                    gridLook_MASource.EditValue = value;
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridLook_MASource_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (display == null)
                e.DisplayText = "Choose MA Source...";
            else
               e.DisplayText = display;
        }

        private void FrmImportTrimList_Load(object sender, EventArgs e)
        {
            sp_MAImport_OrderColorTableAdapter.Fill(dataSetMatarial.sp_MAImport_OrderColor, MALID);
        }

        private void gridLook_Color_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            try
            {
                GridView view = gridLook_Color.Properties.View as GridView;
                if (view.FocusedRowHandle >= 0)
                {
                    int[] rows = view.GetSelectedRows();
                    displaycolor = null;
                    valuecolor = null;
                    if (rows.Count() > 0)
                    {
                        foreach (int i in rows)
                        {
                            if (!view.IsGroupRow(i))
                            {
                                displaycolor += view.GetDataRow(i)["PO"].ToString() + '-' + view.GetDataRow(i)["Color"].ToString() + ", ";
                                valuecolor += view.GetDataRow(i)["OID"].ToString() + ", ";
                            }
                        }
                    }
                    if (displaycolor != null)
                    {
                        displaycolor = displaycolor.Substring(0, displaycolor.Length - 2);
                        valuecolor = valuecolor.Substring(0, valuecolor.Length - 2);
                    }
                    gridLook_Color.EditValue = value;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridLook_Color_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (displaycolor == null)
                e.DisplayText = "Choose Color...";                        
            else
                e.DisplayText = displaycolor; 
        }

        private void bnt__Import_Click(object sender, EventArgs e)
        {
            try
            {
                GridView OIDView = gridLook_Color.Properties.View as GridView;
                GridView MAView = gridLook_MASource.Properties.View as GridView;
                int[] OIDs = OIDView.GetSelectedRows();
                int[] MAList = MAView.GetSelectedRows();
                foreach (int oid in OIDs)
                {
                    foreach (int ma in MAList)
                    {
                        cls.UpdateData("sp_MAListDetailInsert", new string[] { "@MALID", "@OID", "@MAID", "@MADID", "@EstCons", "@CusCons", "@ActCons", "@Wastage", "@Quantity", "@Type","@No" },
                                                               new object[] { MALID, OIDView.GetDataRow(oid)["OID"],MAView.GetDataRow(ma)["MAID"], MAView.GetDataRow(ma)["MADID"], MAView.GetDataRow(ma)["EstCons"],
                                                                          MAView.GetDataRow(ma)["CusCons"],MAView.GetDataRow(ma)["ActCons"],MAView.GetDataRow(ma)["Wastage"],OIDView.GetDataRow(oid)["Quantity"],MAView.GetDataRow(ma)["Type"],1});
                    }
                }
                fr_SD.RunBackroundWorker();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        
    }
}
